package com.boctech.yxt.authcenter.controller;

import com.boctech.yxt.authcenter.entity.JwtProperties;
import com.boctech.yxt.authcenter.service.JwtAuthService;
import com.boctech.yxt.commonsutils.exception.AjaxResponse;
import com.boctech.yxt.commonsutils.exception.CustomException;
import com.boctech.yxt.commonsutils.exception.ExceptionType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@Slf4j
public class JwtAuthController {

    @Autowired
    JwtAuthService jwtAuthService;

    @Autowired
    JwtProperties jwtProperties;

    @PostMapping("/login")
    @ResponseBody
    public AjaxResponse login(@RequestBody Map<String, String> map){
        String username = map.get(jwtProperties.getUserParamName());
        String password = map.get(jwtProperties.getPwdParamName());
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new CustomException(ExceptionType.BAD_REQUEST, "用户名和密码不能为空");
        }
        Map<String, Object> result = null;
        try{
            result = jwtAuthService.login(username, password);
        }catch (CustomException e){
            log.error(e.getMessage());
        }
        return AjaxResponse.success(200, result);
    }

    @GetMapping("/home/sysuser")
    @ResponseBody
    public AjaxResponse find(){
        return AjaxResponse.success(200, "响应成功");
    }

    @GetMapping("/findAll")
    @ResponseBody
    public AjaxResponse findAll(){
        return AjaxResponse.success(200, "响应成功");

    }
}
